//
//  KLSGKindChildViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGKindChildViewController.h"
#import "KLSGCategoryChildTableViewCell.h"
#import "KLSGCategoryTool.h"
#import "KLSGKindModel.h"
#import "KLSGBrifAndVoiceViewController.h"
#import "KLSGDataFail.h"

static NSString *identifier = @"cell";

@interface KLSGKindChildViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger pageId;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) KLSGCategoryTool *tool;
@property (nonatomic, strong) KLSGDataFail *failView;

@end

@implementation KLSGKindChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self subView];
    [self setupRefresh];
    [self setupData];
}

// 懒加载
- (KLSGDataFail *)failView
{
    if (!_failView) {
        _failView = [[KLSGDataFail alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _failView;
}

- (KLSGCategoryTool *)tool
{
    if (!_tool) {
        _tool = [[KLSGCategoryTool alloc] init];
    }
    return _tool;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

// 加载数据
- (void)setupData
{
    NSString *url = kKindChildUrl(self.Id, (long)pageId, self.tname);
    [self.tool setupDataWithUrl:url block:^(NSDictionary *dic) {
        NSArray *array = dic[@"list"];
        for (NSDictionary *tempDic in array) {
            KLSGKindModel *model = [[KLSGKindModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.dataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failDownloadWithBlock:^{
        [self.failView netRequestFailWithBlock:^{
            [self setupData];
            [self.failView hidden];
        }];
        [self.view addSubview:self.failView];
    }];
}

// 下拉刷新，上拉加载
- (void)setupRefresh
{
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    [self.tableView headerBeginRefreshing];
    self.tableView.headerPullToRefreshText = @"下拉可以刷新";
    self.tableView.headerRefreshingText = @"正在加载";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新";
    
    [self.tableView addFooterWithTarget:self action:@selector(footerRefreshing)];
    self.tableView.footerPullToRefreshText = @"下拉可以刷新";
    self.tableView.footerRefreshingText = @"正在加载";
    self.tableView.footerReleaseToRefreshText = @"松开马上刷新";
}
- (void)headerRefreshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _dataArray = nil;
        pageId = 1;
        [self setupData];
        [self.tableView headerEndRefreshing];
    });
}
- (void)footerRefreshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        pageId++;
        if (pageId <= 24) {
            [self setupData];
            [self.tableView footerEndRefreshing];
        }
    });
}

- (void)subView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114) style:(UITableViewStylePlain)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    // 注册cell;
    [self.tableView registerClass:[KLSGCategoryChildTableViewCell class] forCellReuseIdentifier:identifier];
}

#pragma mark -UITableViwDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGCategoryChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (self.dataArray.count >= indexPath.row) {
        KLSGKindModel *model = self.dataArray[indexPath.row];
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        cell.title.text = model.title;
        cell.detail.text = model.intro;
        cell.playCountLabel.text = [NSString stringWithFormat:@"%d万", [model.playsCounts intValue]/ 10000];
        cell.albumCountLabel.text = [NSString stringWithFormat:@"%@集", model.tracksCounts];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGBrifAndVoiceViewController *brifAndVoiceVC = [[KLSGBrifAndVoiceViewController alloc] init];
    KLSGKindModel *model = self.dataArray[indexPath.row];
    brifAndVoiceVC.albumIdString = [NSString stringWithFormat:@"%@", model.albumId];
    brifAndVoiceVC.model = model;
    [self.navigationController pushViewController:brifAndVoiceVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    
}


@end
